﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Cisoft.Cisoft3G.BLL;
using Cisoft.Cisoft3G.Model;

public partial class Business_Cisoft3G_ContactsManage_DefineGroupEdit : WebUI.Page.EditPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindControl();
            BindTree(0, (TreeNode)null);
        }
    }

    #region 绑定控件
    private void BindControl()
    {
        DefineGroupBO bll = new DefineGroupBO();
        DefineGroupMO model = new DefineGroupMO();

        ContactsBO cbo = new ContactsBO();
        ContactsMO cmo = new ContactsMO();

        model = bll.GetModel(id);
        if (model != null)
        {
            txtDefineName.Text = model.DG_Name;
            if (!string.IsNullOrEmpty(model.DG_GroupMember))
            {
                string[] strs = model.DG_GroupMember.TrimEnd(',').Split(',');
                if (strs.Length > 0)
                {
                    foreach (string s in strs)
                    {
                        cmo = cbo.GetModel(int.Parse(s));
                        if (cmo != null)
                        {
                            s2.Items.Add(new ListItem(string.Format("{0}({1})", cmo.CT_Name, cmo.CT_Mobile), cmo.CT_Mobile));
                            hih_right.Value += cmo.CT_Mobile + ",";
                        }
                    }
                }
            }
        }
    }
    #endregion

    #region 修改事件
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        string strError = string.Empty;
        ContactsBO cbo = new ContactsBO();
        ContactsMO cmo = new ContactsMO();
        DefineGroupBO dgbo = new DefineGroupBO();
        DefineGroupMO dgmo = new DefineGroupMO();

        dgmo = dgbo.GetModel(id);
        dgmo.DG_Name = txtDefineName.Text.Trim();
        //dgmo.US_UserID_FK = int.Parse(Session["UserID"].ToString());
        dgmo.US_UserID_FK = 0;
        string member = string.Empty;
        if (!string.IsNullOrEmpty(hih_right.Value))
        {
            string[] strs = hih_right.Value.TrimEnd(',').Split(',');
            foreach (string s in strs)
            {
                if (!string.IsNullOrEmpty(s))
                {
                    cmo = cbo.GetModel(" CT_Mobile=" + s.Trim());
                    if (cmo != null)
                    {
                        member += cmo.CT_ContactID + ",";
                    }
                }
            }
        }
        dgmo.DG_GroupMember = member.TrimEnd(',');

        try
        {
            dgbo.Update(dgmo);
        }
        catch (Exception ex)
        {
            strError = ex.Message;
        }

        if (!string.IsNullOrEmpty(strError))
        {
            PageError(string.Format("修改自定义组失败！失败原因如下：{0}。", strError), "DefineGroup.aspx");
        }
        else
        {
            PageRight("修改自定义组成功！", "DefineGroup.aspx", "点击返回", "");
        }
    }
    #endregion

    #region 返回事件 
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("DefineGroup.aspx");
    }
    #endregion

    #region 绑定TreeView数据
    //绑定TreeView数据
    private void BindTree(int pid, TreeNode node)
    {
        List<ContactGroupMO> modelList = new ContactGroupBO().GetModelList(" CG_ParentID = " + pid + " order by CG_Order desc");
        foreach (ContactGroupMO model in modelList)
        {
            TreeNode tn = new TreeNode();
            if (node == null)
            {
                tn = new TreeNode();
                tn.Text = "<a style='text-decoration:none;' href='GetContactsHandler.ashx?ContactGroupID=" + model.CG_ContactGroupID + "'>" + model.CG_GroupName + "</a>";
                tn.NavigateUrl = "javascript:void(0);";
                tvContactsGroup.Nodes.Add(tn);
                BindTree(model.CG_ContactGroupID, tn);
            }
            else
            {
                tn = new TreeNode();
                tn.Text = "<a style='text-decoration:none;' href='GetContactsHandler.ashx?ContactGroupID=" + model.CG_ContactGroupID + "'>" + model.CG_GroupName + "</a>";
                tn.NavigateUrl = "javascript:void(0);";
                node.ChildNodes.Add(tn);
                BindTree(model.CG_ContactGroupID, tn);
            }
        }
    }
    #endregion
}
